var util = require('../../libs/util.js');
var bdParse = require('../../bdParse/bdParse.js');
var app = getApp();
Page({
    data: {
    	id: null,
    	article: {},			// 文章信息
        articleDetail: '',
        userInfo: {},			// 当前用户信息
        praise: 0,				// 点赞数量
        praiseStatus: 0,	// 点赞状态
        comments: [],			// 文章评论
        showCommentWrite: false,
        commentValue: '',
        commentObj: {
        	user: '',
            pid: 0,
            parentIndex: 0,
            childIndex: 0
        }
    },
    onLoad: function(e) {
        swan.showNavigationBarLoading();
    	this.setData({id: e.id});
        this.init();
    },
    init: function(id) {
        // 通过全局数据获取用户信息
        this.setData({userInfo: app.globalData.userInfo});
        this.queryWhetherPraise(app.globalData.userInfo.nickName);
        this.getArticleDetail();
    },
    // 获取文章信息
    getArticleDetail: function() {
    	var _this = this;
    	// 文章内容
    	util.request({
            url: 'article.article/detail',
            method: 'POST',
            data: {
                id: this.getData('id')
            },
            success: function (res) {
                bdParse.bdParse('articleDetail', 'html', res.result.content, _this, 5);
                _this.setData({article: res.result});
                swan.hideNavigationBarLoading();
                swan.setNavigationBarTitle({title: res.result.title});
            }
        });
        // 点赞数
        util.request({
            url: 'article.admiration/admiration_sum',
            method: 'POST',
            data: {
                id: this.getData('id')
            },
            success: function (res) {
                _this.setData({praise: res.result})
            }
        });
        // 文章评论内容
        util.request({
            url: 'article.comment/lists',
            method: 'POST',
            data: {
                id: this.getData('id'),
                tree: true
            },
            success: function (res) {
                _this.setData({comments: res.result});
            }
        });
    },
    // 查询是否已经点赞
    queryWhetherPraise: function(username) {
    	//if(!username) return;
    	var _this = this;
    	util.request({
            url: 'article.admiration/detail',
            method: 'POST',
            data: {
                article_id: this.data.id,
                username: username
            },
            success: function (res) {
                if(res.code == 200 && res.result) {
                	_this.setData({praiseStatus: res.result.status});
                }
            }
        });
    },
    // 层主头像加载错误时
    bindLayerAvatarError: function(e) {
    	var index = e.target.dataset.errorindex;
    	var comments = this.getData('comments');
    	comments[index].avatar = "../../images/avatar.png";
    	this.setData({comments: comments});
    },
    // 点赞
    handlePraise: function() {
    	var _this = this;
    	util.request({
            url: 'article.admiration/save',
            method: 'POST',
            data: {
                article_id: this.data.id,
                username: this.data.userInfo.nickName,
                avatar: this.data.userInfo.avatarUrl,
                status: (this.data.praiseStatus == 1 ? 0 : 1)
            },
            success: function (res) {
                if(res.code == 200 && res.result) {
                	var praise = _this.data.praise;
                	var message = '点赞成功';
                	if(res.result.status == 1) {
                		praise++;
                	}else{
                		praise--;
                		message = '已取消点赞';
                	}
                	_this.setData({
                		praiseStatus: res.result.status,
                		praise: praise
                	});
                	swan.showToast({
					    title: message,
					    icon: 'none',
					    duration: 1500,
					});
                }else{
                	swan.showToast({
					    title: res.message,
					    icon: 'none',
					    duration: 1500,
					});
                }
            }
        });
    },
    // 写评论
    handleWriteComment: function(e) {
    	this.setData({
            showCommentWrite: true,
            commentObj: {
            	user: e.currentTarget.dataset.user || '',
                pid: e.currentTarget.dataset.pid || 0,
                parentIndex: e.currentTarget.dataset.lindex || 0,
                childIndex: e.currentTarget.dataset.rindex || 0
            }
        });
    },
    handleInputValue(e) {
        this.setData({
            commentValue: e.detail.value
        })
    },
    // 提交评论
    handleSubmitComment: function() {
    	if(!this.data.commentValue) {
    		swan.showToast({
			    title: '请填写您要评论的内容！',
			    icon: 'none',
			    duration: 1000,
			});
			return;
    	}
        var _this = this;
        var comments = this.data.comments;
        var cobj = this.data.commentObj;
    	util.request({
            url: 'article.comment/create',
            method: 'POST',
            data: {
                pid: cobj.pid,
                article_id: this.data.id,
                content: this.data.commentValue,
                username: this.data.userInfo.nickName || '',
                avatar: this.data.userInfo.avatarUrl
            },
            success: function (res) {
            	if(res.code == 200) {
            		res.result.parent_info = {
	            		username: cobj.user
	            	}
	                if(cobj.pid == 0) {
			        	comments.push(res.result);
			        }else{
			        	if(!comments[cobj.parentIndex]._replay_lists) {
			        		comments[cobj.parentIndex]._replay_lists = [];
			        	}
			        	comments[cobj.parentIndex]._replay_lists.push(res.result);
			        }
			        _this.setData({comments: comments});
			        _this.handleCancelSubmit();
			        swan.showToast({
					    title: '评论成功',
					    icon: 'none',
					    duration: 1500,
					});
            	}else{
            		swan.showToast({
					    title: res.message,
					    icon: 'none',
					    duration: 1500,
					});
            	}
            }
        });
    },
    // 取消评论
    handleCancelSubmit() {
    	this.setData({
            showCommentWrite: false,
            commentValue: '',
            commentObj: {
            	user: '',
                pid: 0,
                parentIndex: 0,
                childIndex: 0
            }
        })
    }
})